home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’93 / Mystery Science Mac / INIT sources / hImain.c next >
C/C++ Source or Header  |  1993-09-01  |  4KB  |  206 lines

  1. #include "MacHeaders7"
  2. #include "Utils.h"
  3. #include "MSM.h"
  4.  
  5. #include <Packages.h>
  6.  
  7. #undef SystemSevenOrLater
  8. #define SystemSevenOrLater 1
  9.  
  10. #pragma parameter __D0 __NewGestalt(__D0,__A0)
  11. pascal OSErr __NewGestalt(OSType selector,ProcPtr gestaltFunction) = 0xA3AD;
  12.  
  13. typedef struct sysz {
  14.     long    space;
  15.     void    *DRVR;
  16. } sysz, **syszH;
  17.  
  18. /*** main function ***/
  19.  
  20. static void strip_pc(void) {
  21.     asm {
  22.         move.l    (a7)+,d0
  23.         StripAddress
  24.         move.l    d0,-(a7)
  25.     }
  26. }
  27.  
  28. short gBootTime;
  29.  
  30. long    Gest;
  31.  
  32. short *buffer, gBuf[120];
  33.  
  34. short SoundToPlay(void) {
  35.     if (buffer > gBuf) {
  36.         return *--buffer;
  37.     }
  38.     return 0;
  39. }
  40.  
  41. void QueueSound(short snd) {
  42.     if (buffer != gBuf && gBuf[0] == snd) return;
  43.     if (buffer < &gBuf[100]) {
  44.         *buffer++ = snd;
  45.     }
  46. }
  47.  
  48. static
  49. pascal OSErr MSMGestalt(OSType selector, void **response) {
  50.     void    *oldA4 = CSetUpA4();
  51.  
  52.     if (selector == 'MSM3') {
  53.         *response = &MSMGestalt;
  54.     } else if (selector == 'back') {
  55.         *response = (void *) SoundToPlay();
  56.     } else {
  57.         SetA4(oldA4);
  58.         return gestaltUndefSelectorErr;
  59.     }
  60.  
  61.     SetA4(oldA4);
  62.     return noErr;
  63. }
  64.  
  65. static long myA5;
  66.  
  67. void main() {
  68.     register short    resID;
  69.     register traps    *Patcher;
  70.     uchar            name[128];
  71.  
  72.     strip_pc();
  73.  
  74.     asm {
  75.         ; lea     main,a0    ; commented out; address of code segment is already in a0.
  76.         move.l    a0,d0
  77.         StripAddress
  78.         move.l    d0,a4
  79.     }
  80.     asm {    /* fix up all the SetUpA4's */
  81.         RecoverHandle SYS
  82.         move.l    a0,-(a7)
  83.         jsr        FixA4
  84.         addq    #4,a7
  85.     }
  86.  
  87.         // don't load twice.
  88.     if (Gestalt('MSM3', &Gest) == noErr) return;
  89.  
  90.     if (SysVersion < 0x0700) return;    // system 7 at least, man!
  91.     if (keymap.shift) goto nogo;        // shift key?
  92.  
  93.         // OK, do some real loading.  First, switch to the system heap.
  94.     SetZone(SystemZone());
  95.  
  96.         // OK, detach the MSM region so we can punch it out of the desktop region.
  97.     DetachResource(gMysterious = (void *)GetResource('rgn!', -4048));
  98.  
  99.         // set up a port that we can draw into.
  100.     asm {
  101.         move.l    a5,-(a7)
  102.         move.l    CurrentA5,-(a7)
  103.         lea        myA5,a5
  104.         move.l    a5,CurrentA5
  105.  
  106.         pea        thePort
  107.         InitGraf
  108.  
  109.         move.l    #sizeof(CGrafPort),d0
  110.         NewPtr    SYS
  111.         move.l    a0,MSMPort
  112.         move.l    a0,-(a7)
  113.         cmpi.w    #0x3FFF,ROM85
  114.         bhi.s    @notII
  115.         OpenCPort
  116.         bra.s    @mrg
  117.     notII:OpenPort
  118.     mrg:;
  119.     }
  120.  
  121.     /* if (!MFExists) */ asm {
  122.         move.l    (a7)+,CurrentA5
  123.         move.l    (a7)+,a5
  124.     }
  125.  
  126.         // Patch our traps.  Set the addresses up, then loop betwee them all.
  127.     Patcher = Patches;
  128.     Patcher[myMenuSelect    ].newAddress = (long) real_address(&pMenuSelect);
  129.     Patcher[myDraw1Control    ].newAddress = (long) real_address(&pDraw1Control);
  130.     Patcher[myInitWindows    ].newAddress = (long) real_address(&pInitWindows);
  131.  
  132.     asm {
  133.         move.w    (Patcher),d0
  134. lp:        cmpi.w    #0xA800,d0
  135.         blt.s    @os
  136. tb:        GetTrapAddress    NEWTOOL
  137.         bra.s    @ov
  138. os:        GetTrapAddress    NEWOS
  139. ov:        move.w    (Patcher)+,d0
  140.         move.l    a0,(Patcher)+
  141.         move.l    (Patcher),a0
  142.         tst.l    (Patcher)+
  143.         beq.s    @noptch
  144.         cmpi.w    #0xA800,d0
  145.         blt.s    @OS
  146. TB:        SetTrapAddress    NEWTOOL
  147.         bra.s    @noptch
  148. OS:        SetTrapAddress    NEWOS
  149. noptch:    move.w    (Patcher),d0
  150.         bne.s    @lp
  151.     }
  152.  
  153.         // prep our action-buffering routine.
  154.     buffer = gBuf;
  155.  
  156.         // register our gestalt selector.
  157.     __NewGestalt('MSM3', (void *)&MSMGestalt);    // ignore error code.
  158.  
  159.         // set the zone back.  (Actually, we should probably save it off and restore it
  160.         // to what it was, in case future OS versions don't have an application zone at
  161.         // INIT time.
  162.     SetZone(ApplicZone());
  163.  
  164.     /* install the jGNE patch */
  165.     asm {
  166.         lea        @ptch,a0
  167.         lea        @pj+2,a1
  168.         move.l    JGNEFilter,(a1)
  169.         move.l    a0,JGNEFilter
  170.     }
  171.  
  172.     if (0) {    /* let's not execute the patch code... */
  173. ptch:    asm {
  174.             movem.l    a0-a1/d0-d2,-(a7)
  175.             move.l    a4,-(a7)
  176.             lea        'rga4',a4
  177.             move.l    a1,-(a7)
  178.             move.w    d0,-(a7)
  179.             jsr        pJGNEfilter
  180.             addq    #6,a7
  181.             move.l    (a7)+,a4
  182.             movem.l    (a7)+,a0-a1/d0-d2
  183.     pj:        jmp        0x12345678
  184.         }
  185.     };
  186.  
  187.     asm {
  188.         move.l    a4,a0
  189.         RecoverHandle SYS
  190.         move.l    a0,-(a7)
  191.         DetachResource
  192.     }
  193.  
  194.     ShowINIT(-4064);    // success!
  195.     goto unloadSI;
  196.  
  197. nogo:
  198.     ShowINIT(-4048);
  199.     goto unloadSI;
  200.  
  201. unloadSI:
  202.     UnloadA4Seg(&ShowINIT);    // get rid of the ShowINIT code; we don't need it anymore.
  203.  
  204. out:;
  205.     SetZone(ApplicZone());
  206. }